/* Copyright (c) 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jessies.blogbaboon;

import e.gui.*;
import e.ptextarea.*;
import e.util.*;
import javax.swing.*;

public class BlogBaboonMenuBar extends EMenuBar {
  private final BlogBaboon blogEditor;
  private final EditingWindow editingWindow;
  
  public BlogBaboonMenuBar(BlogBaboon blogEditor, EditingWindow editingWindow) {
    this.blogEditor = blogEditor;
    this.editingWindow = editingWindow;
    
    add(makeFileMenu());
    add(makeEditMenu());
    add(makeHelpMenu());
  }
  
  private JMenu makeFileMenu() {
    JMenu menu = GuiUtilities.makeMenu("File", 'F');
    if (editingWindow == null) {
      menu.add(blogEditor.newPostAction);
      menu.add(blogEditor.editPostAction);
      menu.addSeparator();
      menu.add(blogEditor.deletePostAction);
      if (!GuiUtilities.isMacOs()) {
        menu.addSeparator();
        menu.add(new QuitAction(blogEditor));
      }
    }
    if (editingWindow != null) {
      menu.add(editingWindow.previewAction);
      menu.add(editingWindow.saveDraftAction);
      menu.add(editingWindow.closeAction);
    }
    return menu;
  }
  
  private JMenu makeEditMenu() {
    JMenu menu = GuiUtilities.makeMenu("Edit", 'E');
    if (editingWindow != null) {
      menu.add(PActionFactory.makeUndoAction());
      menu.add(PActionFactory.makeRedoAction());
      menu.addSeparator();
      menu.add(PActionFactory.makeCutAction());
      menu.add(PActionFactory.makeCopyAction());
      menu.add(PActionFactory.makePasteAction());
      menu.add(PActionFactory.makeSelectAllAction());
      menu.addSeparator();
      menu.add(PActionFactory.makeFindAction());
      menu.add(PActionFactory.makeFindNextAction());
      menu.add(PActionFactory.makeFindPreviousAction());
      
      menu.addSeparator();
    }
    blogEditor.getPreferences().initPreferencesMenuItem(menu);
    return menu;
  }
  
  private JMenu makeHelpMenu() {
    HelpMenu helpMenu = new HelpMenu();
    return helpMenu.makeJMenu();
  }
}
